package com.ruoyi.huanjing.controller;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ucar.ma2.Array;
import ucar.nc2.Dimension;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ReadVariable {

    /**
     * 根据变量名读取HDF4/NetCDF文件中特定变量的全部内容
     * @param filePath 文件路径
     * @param variableName 变量名称
     * @return 变量的完整内容及元数据
     */
    @Anonymous
    @GetMapping("/readVariableContent")
    public Map<String, Object> readVariableContent(
            @RequestParam("filePath") String filePath,
            @RequestParam("variableName") String variableName) {
        Map<String, Object> result = new HashMap<>();

        try (NetcdfFile ncFile = NetcdfFile.open(filePath)) {
            Variable var = ncFile.findVariable(variableName);
            if (var == null) {
                result.put("error", "变量不存在: " + variableName);
                return result;
            }

            result.put("name", var.getFullName());
            result.put("dimensions", var.getDimensionsString());
            result.put("dataType", var.getDataType().toString());

            // 区分变量类型
            String variableType = getVariableType(var);
            result.put("variableType", variableType);

            // 读取变量的所有内容
            if (var.getRank() > 0 && var.getSize() > 0) {
                if (var.getRank() == 2) {
                    // 处理2D数据
                    Array dataArray = var.read();
                    int[] shape = dataArray.getShape();
                    Object data = new Object[shape[0]][shape[1]];
                    for (int i = 0; i < shape[0]; i++) {
                        for (int j = 0; j < shape[1]; j++) {
                            ((Object[][]) data)[i][j] = dataArray.getObject(i * shape[1] + j);
                        }
                    }
                    result.put("data", data);
                } else {
                    // 处理其他维度数据
                    Object data = var.read().get1DJavaArray(var.getDataType().getPrimitiveClassType());
                    result.put("data", data);
                }
                result.put("size", var.getSize());
            } else {
                result.put("data", "变量无数据或维度为0");
            }

        } catch (IOException e) {
            result.put("error", "读取变量内容时出错: " + e.getMessage());
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 判断变量类型
     * @param var 变量对象
     * @return 变量类型字符串
     */
    private String getVariableType(Variable var) {
        int rank = var.getRank();
        List<Dimension> dimensions = var.getDimensions();

        if (rank == 0) {
            return "无维度变量";
        } else if (rank == 1) {
            return "1D";
        } else if (rank == 2) {
            // 简单判断 Geo2D，实际可能需要根据维度名称或属性判断
            if (dimensions.stream().anyMatch(d -> d.getName().toLowerCase().contains("lat") || d.getName().toLowerCase().contains("lon"))) {
                return "Geo2D";
            } else {
                return "2D";
            }
        } else {
            return "高维变量";
        }
    }
}
